package com.gh.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.Result;
import com.gh.exception.GgktException;
import com.gh.ggkt.vod.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Eric Fang
 * @since 2022-08-02
 */
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("findAll")
    public Result findAllTeacher() {
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("查询结果成功");
    }

    @DeleteMapping("remove/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean flag = teacherService.removeById(id);
        if(flag){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    @ApiOperation("条件查询分页")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable Long current,
                           @PathVariable Long limit,
                           @ApiParam(name = "teacherVo",value = "查询对象",required = false)
                           @RequestBody TeacherQueryVo teacherQueryVo){
        Page<Teacher> pageable=new Page<>(current,limit);
        if(teacherQueryVo==null){
            IPage<Teacher> page = teacherService.page(pageable, null);
            return Result.ok(page);
        }else {
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            if(StringUtils.isNotEmpty(name)){
                queryWrapper.like(Teacher::getName,name);
            }
            if(level!=null){
                queryWrapper.eq(Teacher::getLevel,level);
            }
            if(StringUtils.isNotEmpty(joinDateBegin)){
                queryWrapper.ge(Teacher::getJoinDate,joinDateBegin);
            }
            if(StringUtils.isNotEmpty(joinDateEnd)){
                queryWrapper.le(Teacher::getJoinDate,joinDateEnd);
            }
            IPage<Teacher> page = teacherService.page(pageable, queryWrapper);
            return Result.ok(page);
        }
    }

    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if(isSuccess){
            return Result.ok(null).message("添加成功");
        }
        return Result.fail(null).message("添加失败");
    }

    @ApiOperation("根据id查询")
    @GetMapping("getTeacher/{id}")
    public Result getTeacherById(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    @ApiOperation("修改讲师")
    @PutMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if(isSuccess){
            return Result.ok(null).message("修改成功") ;
        }
        return Result.fail(null).message("修改失败");
    }

    @ApiOperation("批量删除讲师")
    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids){
        boolean isSuccess = teacherService.removeByIds(ids);
        if(isSuccess){
            return Result.ok(null).message("删除成功");
        }
        return Result.fail(null).message("删除失败");
    }
}

